/**************************************************************************/
/*                                                                        */
/*          	Portions Copyright (C)2013, Jason Whelan                  */
/*                                                                        */
/**************************************************************************/
/*                                                                        */
/*                              WWIV Version 5.0x                         */
/*             Copyright (C)1998-2007, WWIV Software Services             */
/*                                                                        */
/*    Licensed  under the  Apache License, Version  2.0 (the "License");  */
/*    you may not use this  file  except in compliance with the License.  */
/*    You may obtain a copy of the License at                             */
/*                                                                        */
/*                http://www.apache.org/licenses/LICENSE-2.0              */
/*                                                                        */
/*    Unless  required  by  applicable  law  or agreed to  in  writing,   */
/*    software  distributed  under  the  License  is  distributed on an   */
/*    "AS IS"  BASIS, WITHOUT  WARRANTIES  OR  CONDITIONS OF ANY  KIND,   */
/*    either  express  or implied.  See  the  License for  the specific   */
/*    language governing permissions and limitations under the License.   */
/*                                                                        */
/**************************************************************************/

#include "wwiv.h"
#include "TextUI.h"
#include "sys_paths.h"

class MsgsDirCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter Messages Directory:", "Input Path");
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		strcpy(syscfg.msgsdir, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

class GfilesDirCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter GFiles Directory:", "Input Path");
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		strcpy(syscfg.gfilesdir, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

class MenuDirCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter Menus Directory:", "Input Path");
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		strcpy(syscfg.menudir, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

class DataDirCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter Data Directory:", "Input Path");
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		strcpy(syscfg.datadir, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

class DownloadsDirCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter Downloads Directory:", "Input Path");
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		strcpy(syscfg.dloadsdir, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

class TempDirCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter Temporary Directory:", "Input Path");
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		strcpy(syscfg.tempdir, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};

class BatchDirCommand : public UICommand
{
    virtual bool Execute()
    {
        UIDesktop *desktop = UIDesktop::GetDesktop();
		UIInputBox inputBox(desktop, 5, 40, 5, 20, "Enter Batch Directory:", "Input Path");
		
		inputBox.Run();
		
		std::string result = inputBox.GetText();
		
		strcpy(syscfg.batchdir, result.c_str());
		
		desktop->GetStatusBar()->Write(" > " +result);
        return true;
    }
};
bool SystemPaths::Execute()
{
    UIDesktop *desktop = UIDesktop::GetDesktop();
    UIMenu *menu = new UIMenu( "System Paths" );
	menu->Add(new UIMenuItem( "Messages Directory", new MsgsDirCommand ) );
	menu->Add(new UIMenuItem( "GFiles Directory", new GfilesDirCommand ) );
	menu->Add(new UIMenuItem( "Menu Directory", new MenuDirCommand ) );
	menu->Add(new UIMenuItem( "Data Directory", new DataDirCommand ) );
	menu->Add(new UIMenuItem( "Downloads Directory", new DownloadsDirCommand ) );
	menu->Add(new UIMenuItem( "Temporary Directory", new TempDirCommand ) );
	menu->Add(new UIMenuItem( "Batch Directory", new BatchDirCommand ) );
	UIView *currentActiveView = desktop->GetActiveView();
    UIPopupMenu *popup = menu->ShowPopupMenu( desktop, 27, 5 );

    while( true )
    {
        int key = GetKey();
		if(key == 0x1b) break;
		popup->ProcessKeyEvent(key);
    }

    menu->HidePopupMenu();

    delete menu;

	ValidatePaths();

	desktop->SetActiveView(currentActiveView);

    return false;
}

int SystemPaths::GetKey()
{
    while ( true )
    {
        int key = getch();
        if ( key != ERR )
        {
            return key;
        }
    }
    return ERR;
}

void SystemPaths::ValidatePaths()
{
}
